package com.ye.common.security.handle;

import com.alibaba.fastjson.JSON;
import com.ye.common.redis.service.RedisCache;
import com.ye.common.security.dto.SecurityUser;
import com.ye.common.security.service.TokenService;
import com.ye.common.utils.R;
import com.ye.core.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: baby
 * @Date: 2023/10/10 02:34
 * @Description:
 */
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {


    @Autowired
    private RedisCache redisCache;
    @Autowired
    private TokenService tokenService;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        SecurityUser loginUser = tokenService.getLoginUser(request);
        redisCache.deleteObject("login:"+loginUser.getUser().getId());
        ServletUtils.renderString(response, JSON.toJSONString(R.ok("退出成功",null)));
    }
}
